package com.example.capsule_chat0322.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.adapter.FriendApplyAdapter;
import com.example.capsule_chat0322.bean.FriendApplyBean;
import com.example.capsule_chat0322.bean.UserInfoBean;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.interfaces.FriendApplyItemOnClickInterface;
import com.example.capsule_chat0322.request.UserInfoRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

/**
 * @Author zhuzhiming
 * @Date 2022/5/25 20:14
 */
public class FriendApplyActivity extends AppCompatActivity {

    private List<FriendApplyBean> friendApplyBeanList;
    private RecyclerView friendApplyRecyclerView;
    private FriendApplyAdapter friendApplyAdapter;
    private String userId;
    private static final int UPDATE_Friend_CODE = 3;

    private TextView chat_room_list_title, chat_room_list_update;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friend_apply);
        initView();//初始化UI控件
    }

    private void initView() {

        friendApplyRecyclerView = findViewById(R.id.friend_apply_recycler_view);
        chat_room_list_title = findViewById(R.id.chat_room_list_title);
        chat_room_list_update = findViewById(R.id.chat_room_list_update);

        chat_room_list_title.setText("好友申请");
        chat_room_list_update.setVisibility(View.GONE);


        String applyListStr = getIntent().getStringExtra("applyList");
        friendApplyBeanList = new Gson().fromJson(applyListStr, new TypeToken<List<FriendApplyBean>>() {
        }.getType());


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(FriendApplyActivity.this);
        FriendApplyAdapter adapter;
        adapter = (FriendApplyAdapter) GlobalAssistMap.getObject("friendApplyAdapter");
        if (adapter == null) {
            adapter = new FriendApplyAdapter(friendApplyBeanList);
            GlobalAssistMap.putObject("friendApplyAdapter", adapter);
        }
        friendApplyRecyclerView.setLayoutManager(linearLayoutManager);
        adapterAddListener(adapter);
        friendApplyRecyclerView.setAdapter(adapter);


        //doGetFunctionList();
    }


    public void adapterAddListener(FriendApplyAdapter friendApplyAdapter) {

        if (friendApplyAdapter == null) {
            return;
        }

        friendApplyAdapter.setFunctionItemOnClickInterface(new FriendApplyItemOnClickInterface() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position, FriendApplyBean friendApplyBean) {

                UserInfoRequest.doGetUserInfoRequest(new Handler(new Handler.Callback() {
                    @Override
                    public boolean handleMessage(@NonNull Message message) {

                        UserInfoBean userInfoBean = (UserInfoBean) message.obj;
                        GlobalAssistMap.putObject(friendApplyBean.getApplyId() + "Info", userInfoBean);
                        Intent intent = new Intent();
                        intent.setClass(FriendApplyActivity.this, UserInfoActivity.class);
                        if (friendApplyBean.getApplyStatus() == null) {
                            intent.putExtra("friendApply", "true");
                        }
                        intent.putExtra("position", position);
                        intent.putExtra("senderId", Integer.parseInt(friendApplyBean.getApplyId()));
                        intent.putExtra("senderAvatar", friendApplyBean.getFriendAvatarPath());
                        startActivityForResult(intent, UPDATE_Friend_CODE);
                        return true;
                    }
                }), friendApplyBean.getApplyId());

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data != null) {

            if (requestCode == UPDATE_Friend_CODE) {
                int position = data.getIntExtra("position", 0);
                int status = data.getIntExtra("status", 0);
                View view = friendApplyRecyclerView.getChildAt(position);
                FriendApplyBean friendApplyBean = friendApplyBeanList.get(position);
                String statusStr = status == 0 ? "已拒绝" : "已同意";
                String content = "申请人ID: " + friendApplyBean.getApplyId() + "(" + statusStr + ")" + "\n" + "申请说明: " + friendApplyBean.getApplyInstruction();
                TextView friendIfo = view.findViewById(R.id.friend_info);
                friendIfo.setText(content);
                friendApplyBean.setApplyStatus(String.valueOf(status));
                FriendApplyAdapter adapter = new FriendApplyAdapter(friendApplyBeanList);
                GlobalAssistMap.putObject("friendApplyAdapter", adapter);
                Intent intent = new Intent();
                setResult(UPDATE_Friend_CODE, intent);

            }

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImageView remind = (ImageView) GlobalAssistMap.getObject("remind");
        remind.setVisibility(View.GONE);
    }
}
